package MediaLibrary;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JPanel;
/*
 * Autors: Fubu/Robi
 */
public class MediaLibrary {
	
	private static final String[] okTypes = {".mp3", ".wav"};
	
	private ArrayList<String> list;
	private ArrayList<File> files;
	
	public MediaLibrary() {
		this.list = new ArrayList<String>();
		files = new ArrayList<File>();
	}
	
	//open up the filechooser and addFile
	public ArrayList<String> openFile() {
		
		JFileChooser fileChooser = new JFileChooser();
		
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int result = fileChooser.showOpenDialog(null);
		
		if(result == JFileChooser.CANCEL_OPTION) { return null;}
		
		ArrayList<File> media = new ArrayList<File>();
		
		addFile(fileChooser.getSelectedFile(), media);
		for (File file : media) {
			list.add(file.getName());
			files.add(file);
		}
		
		return list;
	}
	//add file and its name to the lists
	private void addFile(File file, ArrayList<File> media) {
		
		File[] array = file.listFiles();
		
		for(File f: array) {
			
			if(f.isDirectory()) {
				addFile(f, media);
			} else if(isOkayFileType(f)) {
				media.add(f);
			}
		}
	}
	
	public void clearList(){
		list.clear();
		files.clear();
	}
	
	public ArrayList<File> getFiles() {
		return files;
	}
	
	private boolean isOkayFileType(File f){
		for(String s : okTypes){
			if(f.toString().endsWith(s)){
				return true;
			}
		}
		return false;
	}
}
